package com.funong.matrix.biz.massage.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.funong.matrix.biz.base.dao.SysInmailDao;
import com.funong.matrix.biz.base.dao.SysTagDao;
import com.funong.matrix.biz.base.dao.SysUserDao;
import com.funong.matrix.biz.base.entity.SysInmailEntity;
import com.funong.matrix.biz.base.entity.SysSendTaskEntity;
import com.funong.matrix.biz.base.entity.SysTagEntity;
import com.funong.matrix.biz.base.entity.SysUserEntity;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.dto.Result;
import com.funong.matrix.core.utils.DateTimeUtil;
import com.funong.matrix.core.utils.JwtUtils;
import com.funong.matrix.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : NaNa
 * @since : 2023-05-19 16:56
 **/
@Slf4j
@Service
public class SysInmailDomainDao {

    @Resource
    private SysInmailDao inmailDao;

    @Resource
    private SysUserDao sysUserDao;

    public void saveMessageEntity(SysSendTaskEntity sendTaskEntity){
        String currentUserName = JwtUtils.getCurrentUserName();
        if(StringUtils.isNotBlank(currentUserName)){
            sendTaskEntity.setCreatedBy(currentUserName);
        }
        sendTaskEntity.setCreatedAt(DateTimeUtil.now())
                .setUpdatedAt(DateTimeUtil.now());
        inmailDao.saveMessageEntity(sendTaskEntity);
    }

    public Result queryInmail(QueryDTO<SysInmailEntity> queryDTO){
        return inmailDao.queryInmail(queryDTO);
    }

    public Result readInmail(List<Integer> idList){
        return inmailDao.readInmail(idList);
    }

    /**
     * 构建发送信息并进行发送操作
     * @param permissionCode
     * @param title
     * @param content
     */
    @Transactional(rollbackFor = Exception.class)
    public void sendInMailMessage(String permissionCode, String title, String content) {
        // 1.获取这个权限编码有多少个人
        List<SysUserEntity> userEntities = sysUserDao.queryUserByPermission(permissionCode);
        if (userEntities != null && userEntities.size() > 0) {
            // 构建消息体进行发送
            for(SysUserEntity sysUser : userEntities) {
                SysInmailEntity inmailEntity = new SysInmailEntity();
                inmailEntity.setPlanSendTime(DateTimeUtil.now());
                inmailEntity.setSender("admin");
                inmailEntity.setSenderName(sysUser.getName());
                inmailEntity.setReceiver(sysUser.getId().toString());
                inmailEntity.setReceiverType(sysUser.getName());
                inmailEntity.setTitle(title);
                inmailEntity.setContent(content);
                inmailEntity.setIsRead(0);
                String currentUserName = JwtUtils.getCurrentUserName();
                if(StringUtils.isNotBlank(currentUserName)){
                    inmailEntity.setCreatedBy(currentUserName);
                }
                inmailEntity.setCreatedAt(DateTimeUtil.now())
                        .setUpdatedAt(DateTimeUtil.now());
                inmailEntity.setStatus(1);
                inmailEntity.setSystemId(1);
                inmailDao.save(inmailEntity);
            }
        }
    }
}
